package com.company.blockqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @description: BlockingQueue的4种API调用
 * @author: ruoan
 * @date: 2020/12/10 22:30
 */
public class BlockingQueueDemo {


    public static void main(String[] args) {


        //addAndRemoveGroup();

        //offerAndPoll();

        //putAndTake();


        //offerAndPollTimedVersion();

    }

    /**
     * 不报异常的版本的超时版本
     */
    private static void offerAndPollTimedVersion() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        try {
            System.out.println(blockingQueue.offer("x", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("y", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("z", 3L, TimeUnit.SECONDS));
            System.out.println(blockingQueue.offer("o", 3L, TimeUnit.SECONDS)); //3s后，放不进去，直接返回false

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 阻塞版本
     */
    private static void putAndTake() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        try {
            blockingQueue.put("a");
            blockingQueue.put("b");
            blockingQueue.put("c");
            //blockingQueue.put("D");
            System.out.println("====================");
            blockingQueue.take();
            blockingQueue.take();
            blockingQueue.take();
            blockingQueue.take();
            System.out.println("===============");


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 不报异常版本
     */
    private static void offerAndPoll() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.peek()); //队头没有元素，返回null


        boolean a = blockingQueue.offer("a");
        System.out.println(a);
        boolean b = blockingQueue.offer("b");
        System.out.println(b);
        boolean c = blockingQueue.offer("c");
        System.out.println(c);

        System.out.println(blockingQueue.peek());


        String ret1 = blockingQueue.poll();
        System.out.println(ret1);
        String ret2 = blockingQueue.poll();
        System.out.println(ret2);
        String ret3 = blockingQueue.poll();
        System.out.println(ret3);
        String ret4 = blockingQueue.poll();  //没有元素可以弹出，返回null
        System.out.println(ret4);
    }


    /**
     * 报异常版本
     */
    private static void addAndRemoveGroup() {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);



        System.out.println(blockingQueue.add("1"));

        System.out.println(blockingQueue.add("2"));
        System.out.println(blockingQueue.add("3"));
        //System.out.println(blockingQueue.add("4"));
        String element = blockingQueue.element();
        System.out.println(element);
        System.out.println(blockingQueue.remove()); // 移除队列头元素 a
        System.out.println(blockingQueue.remove()); // 移除队列头元素 b
        System.out.println(blockingQueue.remove());
    }

}
